package com.chmingx.boot;

import com.chmingx.boot.bean.Person;
import com.chmingx.boot.bean.Pet;
import com.chmingx.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication // @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
public class MainApplication {
    public static void main(String[] args) {
        // 1. 返回IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        // 2. 查看容器中的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }

        // 3. 从容器中获取组件
        Pet pet1 = run.getBean("pet", Pet.class);
        Pet pet2 = run.getBean("pet", Pet.class);
        System.out.println(pet1 == pet2); // true, 同一对象，单例

        // 4. 配置类本身也是组件
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        // 5. 测试@Component
        Person person = run.getBean(Person.class);
        System.out.println(person);

    }
}
